﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace SolutionCrapFinder
{
    public partial class MainForm : Form
    {
        private ContextMenuStrip listboxContextMenu;
        private const string contextItemRemove = "Remove item(s)";
        private const string contextItemMoveTo = "Move item(s) to \"\\_crap\" folders";
        private const string contextItemSetIgnored = "Move to ignore list";

        string m_fileSettings = null;
        SCFProjectSettings m_settings = new SCFProjectSettings();
        List<string> m_allCrapFiles = new List<string>();

        public MainForm()
        {
            InitializeComponent();
            FillRecentProjects();
            PrepareCrapFileListContextMenu();
        }

        private void listboxContextMenu_Opening(object sender, CancelEventArgs e)
        {
            listboxContextMenu.Items.Clear();
            listboxContextMenu.Items.Add(contextItemRemove);
            listboxContextMenu.Items.Add(contextItemMoveTo);
            listboxContextMenu.Items.Add(contextItemSetIgnored);
        }

        private void listboxContextMenu_ItemClicked(Object sender, ToolStripItemClickedEventArgs e)
        {
            List<string> toRemove = new List<string>();
            switch (e.ClickedItem.Text)
            {
            case contextItemRemove:
            	break;
            case contextItemMoveTo:
                foreach (string s in crapFileListBox.SelectedItems)
                {
                    Miscellaneous.MoveFileToFolder(s, "_crap");
                    toRemove.Add(s);
                }                
            	break;
            case contextItemSetIgnored:
                foreach (string s in crapFileListBox.SelectedItems)
                {
                    m_settings.m_ignoredFiles.Add(s);
                    toRemove.Add(s);
                }
                break;
            default:
                Debug.Assert(false);
                break;
            }

            foreach (string s in toRemove)
            {
                crapFileListBox.Items.Remove(s);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            List<string> allCrapFiles = new List<string>();
            allCrapFiles.AddRange(Directory.EnumerateFiles(m_settings.m_directory, "*.*", SearchOption.AllDirectories));
            Miscellaneous.ToLower(allCrapFiles);

            foreach (string sln in m_settings.m_solutions)
            {
                SolutionItem solution = new SolutionItem(sln, m_settings.m_directory);
                //solution.SetIgnoredItems(m_settings.m_ignoredFiles, null);
                solution.ProcessSolution();
                var tmp = solution.GetCrapFiles();
                allCrapFiles = Miscellaneous.Intersect(allCrapFiles, tmp);
            }

            m_allCrapFiles = allCrapFiles;
            ShowCrapFiles();
            
            /*
            StringBuilder sbCommon = new StringBuilder();

            // prepare report by crap files
            StringBuilder sb = new StringBuilder();
            foreach (string s in allCrapFiles)
            {
                sb.AppendLine(s);
            }

            sbCommon.Append(sb.ToString());

            // prepare report by projects
            StringBuilder sbProjectReports = new StringBuilder();
            List<ProjectInfo> projects = solution.GetProjects();
            foreach (ProjectInfo proj in projects)
            {
                sbProjectReports.AppendFormat("Project: {0}\n", proj.projectName);
                sbProjectReports.Append(proj.errorReport.ToString());
                sbProjectReports.Append("\n\n");
            }
            sbCommon.Append("\n\n\n");
            sbCommon.Append(sbProjectReports.ToString());

            richTextBox1.Text = sbCommon.ToString();
             * */
        }
        
        private void button4_Click(object sender, EventArgs e)
        {
            ShowCrapFiles();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveProject(false);
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveProject(true);
        }

        private void openProjectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.CheckFileExists = true;
            dlg.Filter = "SCF Project files (*.scfproj)|*.scfproj";
            dlg.ShowDialog();
            m_settings = SCFProjectSettings.Load(dlg.FileName);
            m_settings.m_IsDirty = true;
            m_fileSettings = dlg.FileName;
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CloseProject();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CloseProject();
            Close();
        }

        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SCFProjectSettingsForm settingsForm = new SCFProjectSettingsForm();
            settingsForm.Settings = m_settings;
            DialogResult res = settingsForm.ShowDialog();
            if (res == DialogResult.OK)
            {
                m_settings = settingsForm.Settings;
                if (m_fileSettings != null)
                {
                    SCFProjectSettings.Save(m_fileSettings, m_settings);
                }
                else
                {
                    m_settings.m_IsDirty = true;
                }
            } 
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            CloseProject();
        }
    }
}
